package com.ruoyi.patrol.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.PatrolConstans;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.patrol.domain.PtLineStaff;
import com.ruoyi.patrol.domain.PtLineTask;
import com.ruoyi.patrol.service.IPtLineStaffService;
import com.ruoyi.patrol.service.IPtLineTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 巡查排班(行政)Controller
 *
 * @author mlb
 * @date 2022-11-21
 */
@RestController
@RequestMapping("/patrol/line_staff")
public class PtLineStaffController extends BaseController {
    @Autowired
    private IPtLineStaffService ptLineStaffService;
    @Autowired
    IPtLineTaskService ptLineTaskService;

    /**
     * 查询巡查排班(行政)列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_staff:list')")
    @GetMapping("/list")
    public TableDataInfo list(PtLineStaff ptLineStaff) {
        startPage();
        List<PtLineStaff> list = ptLineStaffService.selectPtLineStaffList(ptLineStaff);
        return getDataTable(list);
    }

    /**
     * 查询巡查排班(行政)列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_staff:list')")
    @GetMapping("/list/nopage")
    public List<PtLineStaff> list() {
        List<PtLineStaff> list = ptLineStaffService.selectPtLineStaffList(new PtLineStaff());
        return list;
    }

    /**
     * 导出巡查排班(行政)列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_staff:export')")
    @Log(title = "巡查排班(行政)", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PtLineStaff ptLineStaff) {
        List<PtLineStaff> list = ptLineStaffService.selectPtLineStaffList(ptLineStaff);
        ExcelUtil<PtLineStaff> util = new ExcelUtil<PtLineStaff>(PtLineStaff.class);
        return util.exportExcel(list, "line_staff");
    }

    /**
     * 获取巡查排班(行政)详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_staff:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ptLineStaffService.selectPtLineStaffById(id));
    }

    /**
     * 新增巡查排班(行政)
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_staff:add')")
    @Log(title = "巡查排班(行政)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PtLineStaff ptLineStaff) {
        for (String date : ptLineStaff.getDates()) {
            PtLineStaff query = new PtLineStaff();
            query.setLineId(ptLineStaff.getLineId());
            query.setGroupId(ptLineStaff.getGroupId());
//            query.setDate(PtLineStaff.getDate());
            query.setDate(DateUtils.parseDate(date));
            long count = ptLineStaffService.selectCount(query);
            if (count > 0) {
                return AjaxResult.error("该小组在" + date + "已分配此路线");
            }
        }

        return toAjax(ptLineStaffService.insertPtLineStaff(ptLineStaff));
    }

    /**
     * 修改巡查排班(行政)
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_staff:edit')")
    @Log(title = "巡查排班(行政)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PtLineStaff ptLineStaff) {
        PtLineStaff query = new PtLineStaff();
        query.setLineId(ptLineStaff.getLineId());
        query.setStaffId(ptLineStaff.getStaffId());
        query.setDate(ptLineStaff.getDate());
        List<PtLineStaff> list = ptLineStaffService.selectPtLineStaffList(query);
        long count = list.stream().filter(e -> !e.getId().equals(ptLineStaff.getId())).count();
        if (count > 0) {
            return AjaxResult.error("该人员当天已分配此路线");
        }

        return toAjax(ptLineStaffService.updatePtLineStaff(ptLineStaff));
    }

    /**
     * 删除巡查排班(行政)
     */
    @PreAuthorize("@ss.hasPermi('patrol:line_staff:remove')")
    @Log(title = "巡查排班(行政)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        PtLineStaff ptLineStaff = ptLineStaffService.selectPtLineStaffById(ids[0]);
        if (ptLineStaff.getPatrolStatus() != PatrolConstans.PATROL_SCHEDULE_STATUES_UNDO) {
            return AjaxResult.error("该排班已经开始巡查，不能删除");
        }
        PtLineTask ptLineTask = new PtLineTask();
        ptLineTask.setLineId(ptLineStaff.getLineId());
        ptLineTask.setGroupId(ptLineStaff.getGroupId());
        ptLineTaskService.deletePtLineTaskByLineIdAndGroupId(ptLineTask);
        return toAjax(ptLineStaffService.deletePtLineStaffByIds(ids));
    }
}
